<?php

/**
 * Url Class
 *
 * Class provide work and manage the entire application
 */

class Url {

	private $_baseUrl;
	private $_staticUrls;
	public $_defaultController;

	function __construct($staticUrls=null,$defaultController="main"){
		$this->_staticUrls = $staticUrls;
		$this->_defaultController = $defaultController;
	}

	public function getBaseUrl() {

		if ($this->_baseUrl === null){
			$this->_baseUrl =  explode("/",ltrim(str_replace(dirname($_SERVER['SCRIPT_NAME']),'',$_SERVER['REQUEST_URI']),"/"));
		}

		return $this->_baseUrl;
	}

	public function getPath($number=1){

		$path = $this->getBaseUrl();
		$number--;
		if (isset($path[$number])){
			return $path[$number];
		}
		
		return "";
	}

	public function parse(){

		$controllerPath = $this->getPath(1);

		if (!isset($this->_staticUrls[$controllerPath])){
			$controller = ($controllerPath ? $controllerPath : $this->_defaultController)."Controller";
			$action = "action".($this->getPath(2) ? ucfirst($this->getPath(2)) : "Index");
		} else {
			if (is_array($this->_staticUrls[$controllerPath])){
				$controller = $this->_staticUrls[$controllerPath][0]."Controller";
				$action = "action".$this->_staticUrls[$controllerPath][1];
			} else {
				$controller = $this->_staticUrls[$controllerPath]."Controller";
				$action = "action".($this->getPath(2) ? ucfirst($this->getPath(2)) : "Index");
			}
		}

		if (!is_file(APP_PATH.DS."controllers".DS.$controller.".php")){
			$controller = "errorController";
		}

		return array($controller,$action);
	}
}

?>